/**
 * 工具类
 * @module tools
 */
/* eslint-disable */
import { Toast } from "mand-mobile"

let tools = {
    getUrlKey: function () {
        console.log(location, 'search')
        let url = decodeURI(decodeURI(location.search)); //获取url中"?"符后的字串
        let theRequest = new Object();
        if (url.indexOf("?") != -1) {
            let str = url.substr(1);
            let strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    },
    getOrderMoneyByTime(orderTime, BASEACCOUNT) {
        if (orderTime <= BASEACCOUNT.basicTime * 60) {
            return this.getFloorStr(BASEACCOUNT.basicMoney)
        }
        // 订单时间 = 总订单时间 - 基础时间
        // 价格 = 基础价格 + (订单时间(分钟)/叠加时间 + [订单时间(ms) % 叠加时间(ms) != 0 ? 1 : 0]) * 叠加价格
        let extraTime = orderTime - BASEACCOUNT.basicTime * 60;
        let money = BASEACCOUNT.basicMoney + Math.floor((extraTime / 60 / BASEACCOUNT.addTime + 1)) * BASEACCOUNT.addMoney;
        return this.getFloorStr(money)
    },
    getFloorStr(num) {
        num += '';
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
        if (/^0+/) //清除字符串开头的0
            num = num.replace(/^0+/, '');
        if (!/\./.test(num)) //为整数字符串在末尾添加.00
            num += '.00';
        if (/^\./.test(num)) //字符以.开头时,在开头添加0
            num = '0' + num;
        num += '00'; //在字符串末尾补零
        num = num.match(/\d+\.\d{2}/)[0];
        return num
    },
    zeroPadding(value) {
        if (value < 0) {
            return false
        }
        return value < 10 ? `0${value}` : `${value}`;
    },
    getObjectURL: function (file) {
        let url = null;
        if (window.createObjectURL !== undefined) { // basic
            url = window.createObjectURL(file);
        } else if (window.URL !== undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL !== undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    },
    getOptions: function (url) {
        //定义一个空对象
        var obj = {};
        //如果字符串里面存在?
        if (url.indexOf("?") != -1) {
            //从url的索引1开始提取字符串
            var str = url.substr(url.indexOf("?") + 1);
            //如果存在&符号，则再以&符号进行分割
            var arr = str.split("&");
            //遍历数组
            for (var i = 0; i < arr.length; i++) {
                obj[arr[i].split("=")[0]] = unescape(arr[i].split("=")[1]);
            }
        }
        return obj
    },
    getPosition() {
        return new Promise((resolve, reject) => {
            let data = {
                lat: 29.8172,
                lng: 121.547
            }
            resolve(data)
            /*if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    let latitude = position.coords.latitude
                    let longitude = position.coords.longitude
                    let data = {
                        latitude: latitude,
                        longitude: longitude
                    }
                    resolve(data)
                }, function () {
                    reject(arguments)
                })
            } else {
                reject('你的浏览器不支持当前地理位置信息获取')
            }*/
        })
    }
};

export default tools;
